package com.shizhanzhe.gxb.vip.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.gxb.vip.adapter.NoticeStudyAdapter;
import com.shizhanzhe.gxb.vip.bean.Notice;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * Created by YJ on 2019/1/21.
 */
public class NoticeActivity extends SwipeBackActivity {
    @BindView(R.id.backarea)
    RelativeLayout backarea;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.lv)
    RecyclerView lv;
    @BindView(R.id.nodata)
    ImageView nodata;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    private int page = 1;
    private NoticeStudyAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarView(R.id.bar_0_view)
                .init();
        final int type = getIntent().getIntExtra("type", 0);
        findViewById(R.id.backarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        lv.setLayoutManager(new LinearLayoutManager(NoticeActivity.this));
        lv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mAdapter = new NoticeStudyAdapter(NoticeActivity.this);
        lv.setAdapter(mAdapter);
        mAdapter.setListener(new NoticeStudyAdapter.NoticeListener() {
            @Override
            public void itemclisk(Notice.DataBeanX.DataBean bean) {
                Intent intent = new Intent(NoticeActivity.this, NoticeDetailActivity.class);
                intent.putExtra("title", bean.getTitle());
                intent.putExtra("id", bean.getId());
                intent.putExtra("type", type);
                intent.putExtra("details",bean.getDetails());
                intent.putExtra("content",bean.getContent());
                startActivity(intent);
            }
        });
        //设置 Header 样式
        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                getData(type);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData(type);
            }
        });
        if (type == 1) {
            topTitle.setText("学习中心");
        } else if (type == 2) {
            topTitle.setText("规则中心");
        }
        getData(type);
    }

    private void getData(final int type) {
        String path = "";
        if (type == 1) {
            path = Path.vipstudy(page);
        } else if (type == 2) {
            path = Path.vipregular(page);
        }
        OkHttpDownloadJsonUtil.downloadJson(this, path, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                refresh.finishRefresh();
                refresh.finishLoadMore();
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        Gson gson = new Gson();
                        final List<Notice.DataBeanX.DataBean> list = gson.fromJson(json, Notice.class).getData().getData();
                        if (list.size() == 0) {
                            if (page == 1) {
                                nodata.setVisibility(View.VISIBLE);
                                lv.setVisibility(View.GONE);
                            } else {
                                Toast.makeText(NoticeActivity.this, "已加载全部", Toast.LENGTH_SHORT).show();
                            }
                        }
                        mAdapter.updateData(page,list);
                    } catch (Exception e) {

                    }
                } else {
                    Toast.makeText(NoticeActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy(); //必须调用该方法，防止内存泄漏
    }
}
